package com.leqicheng.tenon.util;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import org.apache.cordova.CordovaPlugin;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.entity.StringEntity;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.leqicheng.tenon.login.PopWindow;
import com.leqicheng.tenon.model.EntryInfoModel;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class GetTimeDiffrenceRequest {

    private final Context mContext;
    private HttpEntity entity = null;
    public final String REQUEST_FAILED = "10001";
    private static String DEFAULTVERSION = "1/";
    private String mApiVersion;
    private PopWindow mPopWindowContext;
    private CordovaPlugin mCordovaPlugin;
    private Activity mActivity;
    private String nowUrl;

    public GetTimeDiffrenceRequest(Context context, Activity activity) {
        this.nowUrl = UrlConfig.getNow();
        this.mContext = context;
        mActivity = activity;
    }

    public void AsyncHttpClientGetOnBack() {

        AsyncHttpClient client = new AsyncHttpClient();
        client.setMaxRetriesAndTimeout(0, 5000);
        String url = this.nowUrl;

        client.get(mContext, url, new AsyncHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers,
                    byte[] responseBody) {
                // TODO Auto-generated method stub
                int timeDiffrence = 0;
                try {
                    String responseBodyInString = new String(responseBody,
                            "UTF-8");
                    timeDiffrence = new Integer(responseBodyInString)
                            - (int) System.currentTimeMillis() / 1000;
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                EntryInfoModel model = EntryInfoModel.getInstance();
                model.setTimeDiffrence(timeDiffrence);

            }

            @Override
            public void onFailure(int arg0, Header[] arg1, byte[] arg2,
                    Throwable arg3) {
                // TODO Auto-generated method stub

            }
        });
    }

}
